package com.dev;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Space;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
import com.ima.ImageManipulationPage;
import com.log;
import java.io.File;
import java.io.FileOutputStream;
public class QueryNewImage extends LinearLayout implements OnClickListener,OnEditorActionListener{
	public static QueryNewImage qni;
	WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
	ColorTextButton btSure;
	LinearLayout titlebar;
	LinearLayout line1;
	LinearLayout line2;
    LinearLayout line3;
	LinearLayout bts;
	IconViewClose ivc;
	EditText editor1,editor2,editor3;
	ShowSoftInput ssi;
	TextView title;
	TextView tv1,tv2,tv3;
	File destination;
	MainActivity ma;
    public QueryNewImage(MainActivity ma){
		super(ma);
		this.ma=ma;
		setOrientation(VERTICAL);
		setBackgroundDrawable(new GUI.BorderDrawable());
		title=new TextView(ma);
		title.setText("新建图像");
		title.setGravity(Gravity.CENTER_VERTICAL);
		title.setTextSize(TypedValue.COMPLEX_UNIT_DIP,13);
		ssi=new ShowSoftInput(ma);
		ssi.setOnClickListener(this);
		ivc=new IconViewClose(ma);
		ivc.setOnClickListener(this);
		titlebar=new LinearLayout(ma);
		titlebar.setOrientation(HORIZONTAL);
		titlebar.setBackgroundDrawable(new GUI.BorderDrawable());
		titlebar.addView(title,-2,-1);
		titlebar.addView(new Space(ma),new LayoutParams(0,-1,1));
		titlebar.addView(ssi,-2,-1);
		titlebar.addView(ivc,-2,-1);
		titlebar.setPadding(9,0,9,0);
		editor1=new UnderlineSingleLineEditor(ma);
		editor1.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
		editor1.setPadding(0,0,9,0);
		editor1.setOnEditorActionListener(this);
		editor2=new UnderlineSingleLineEditor(ma);
		editor2.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
		editor2.setPadding(0,0,9,0);
		editor2.setOnEditorActionListener(this);
        editor3=new UnderlineSingleLineEditor(ma);
        editor3.setPadding(0,0,9,0);
        editor3.setOnEditorActionListener(this);
		int hintWidth=(int)(28*ma.dp);
		tv1=new TextView(ma);
		tv1.setTextSize(13);
		tv1.setGravity(Gravity.CENTER_VERTICAL);
		tv1.setTypeface(Typeface.MONOSPACE);
		tv1.setText("宽：");
		tv2=new TextView(ma);
		tv2.setTextSize(13);
		tv2.setGravity(Gravity.CENTER_VERTICAL);
		tv2.setTypeface(Typeface.MONOSPACE);
		tv2.setText("高：");
        tv3=new TextView(ma);
        tv3.setTextSize(13);
        tv3.setGravity(Gravity.CENTER_VERTICAL);
        tv3.setTypeface(Typeface.MONOSPACE);
        tv3.setText("文件名：");
		LayoutParams flexlp=new LayoutParams(0,-1,1);
		line1=new LinearLayout(ma);
		line1.setOrientation(0);
		line1.setPadding(9,0,9,0);
		line1.addView(tv1,hintWidth,-1);
		line1.addView(editor1,flexlp);
		line2=new LinearLayout(ma);
		line2.setOrientation(0);
		line2.setPadding(9,0,9,0);
		line2.addView(tv2,hintWidth,-1);
		line2.addView(editor2,flexlp);
        line3=new LinearLayout(ma);
        line3.setOrientation(0);
        line3.setPadding(9,0,9,0);
        line3.addView(tv3,hintWidth,-1);
        line3.addView(editor3,flexlp);
		btSure=new ColorTextButton(ma);
		btSure.setText("创建");
		btSure.setOnClickListener(this);
		LayoutParams btlp=new LayoutParams(0,-1,1f);
		bts=new LinearLayout(ma);
		bts.setOrientation(HORIZONTAL);
		bts.addView(btSure,btlp);
		addView(titlebar,-1,(int)(30*ma.dp));
		int lineheight=(int)(30*ma.dp);
		addView(line1,-1,lineheight);
		addView(line2,-1,lineheight);
        addView(line3,-1,lineheight);
		addView(bts,-1,lineheight);
	}
    public void show(){
		destination=null;
		editor1.setText("96");
		editor2.setText("96");
		wlp.type=wlp.TYPE_APPLICATION;
		wlp.width=ma.wp*4/5;
		wlp.height=-2;
		ma.wm.addView(this,wlp);
	}
	public void show(File destination){
		this.destination=destination;
		wlp.type=wlp.TYPE_APPLICATION;
		wlp.width=ma.wp*4/5;
		wlp.height=-2;
		ma.wm.addView(this,wlp);
	}
	int sizeLim=10000;
	@Override
	public boolean onEditorAction(TextView textView, int p, KeyEvent keyEvent) {
		String userinput=textView.getText().toString();
		if(textView==editor1){
			try{
				int num=Integer.parseInt(userinput);
				if(num>0){
					if(num<sizeLim){
						return false;
					}else{
						Toast.makeText(ma,"宽度过大",0).show();
					}
				}else{
					Toast.makeText(ma,"宽度不能<=0",0).show();
				}
			}catch(NumberFormatException nfe){
				Toast.makeText(ma,"无效的输入",0).show();
			}catch(Exception e){
				log.alert(e.toString());
			}
		}else if(textView==editor2){
			int height=Integer.parseInt(userinput);
			try{
				if(height>0){
					if(height<sizeLim){
                        return false;
					}else{
						Toast.makeText(ma,"高度过大",0).show();
					}
				}else{
					Toast.makeText(ma,"高度不能<=0",0).show();
				}
                
			}catch(NumberFormatException nfe){
				Toast.makeText(ma,"无效的输入",0).show();
				return true;
			}catch(Exception e){
				log.alert(e.toString());
			}
			int width;
			try{
				width=Integer.parseInt(editor1.getText().toString());
				if(width>0){
					if(width<sizeLim){
						editor2.clearFocus();
						ImageManipulationTab imt=new ImageManipulationTab(ma);
						Bitmap image=Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
						ImageManipulationPage imp=new ImageManipulationPage(ma,image);
						imt.imp=imp;
						imt.setOnClickListener(ma.gui.TabOnClick);
						imt.setText("新图像");
						ma.gui.tabs.addView(imt,TabBar.tablp);
						ma.gui.TabOnClick.onClick(imt);
						ma.wm.removeView(this);
						return true;
					}else{
						Toast.makeText(ma,"宽度过大",0).show();
					}
				}else{
					Toast.makeText(ma,"宽度必须>0",0).show();
				}
			}catch(NumberFormatException nfe){
				Toast.makeText(ma,"无法识别输入的宽度",0).show();
				return true;
			}catch(Exception e){
				log.alert(e.toString());
			}
		}else if(textView==editor3){
            String name=editor3.getEditableText().toString();
            if(name.contains("/")){
                Toast.makeText(ma,"文件名不能包含/",0).show();
                return true;
            }else if(name.isEmpty()){
                
            }else{
                File f=new File(destination,name);
                if(f.exists()){
                    Toast.makeText(ma,"文件已存在",0).show();
                    return true;
                }
            }
            int width;
            int height;
            try{
                width=Integer.parseInt(editor1.getText().toString());
            }catch(NumberFormatException nfe){
                Toast.makeText(ma,"无法识别的宽度输入",0).show();
                return true;
            }
            try{
                height=Integer.parseInt(editor2.getText().toString());
            }catch(NumberFormatException nfe){
                Toast.makeText(ma,"无法识别的高度输入",0).show();
                return true;
            }
            if(width<=0){
                Toast.makeText(ma,"宽度必须>0",0).show();
                return true;
            }else if(width>=sizeLim){
                Toast.makeText(ma,"宽度过大",0).show();
                return true;
            }
            if(height<=0){
                Toast.makeText(ma,"高度必须>0",0).show();
                return true;
            }else if(height>=sizeLim){
                Toast.makeText(ma,"高度过大",0).show();
                return true;
            }
        }
		return false;
	}
	@Override
	public void onClick(View v){
		if(v==ivc){//onClick
			ma.wm.removeView(this);
		}else if(v==ssi){//onClick
			if(editor1.hasFocus()){
				ma.imm.showSoftInput(editor1,0);
				editor1.selectAll();
			}else if(editor2.hasFocus()){
				ma.imm.showSoftInput(editor2,0);
				editor2.selectAll();
			}else if(editor1.requestFocus()){
				ma.imm.showSoftInput(editor1,0);
				editor1.selectAll();
			}
		}else if(v==btSure){//onClick
			int width;
			int height;
			try{
				width=Integer.parseInt(editor1.getText().toString());
			}catch(NumberFormatException nfe){
				Toast.makeText(ma,"无法识别的宽度输入",0).show();
				return;
			}
			try{
				height=Integer.parseInt(editor2.getText().toString());
			}catch(NumberFormatException nfe){
				Toast.makeText(ma,"无法识别的高度输入",0).show();
				return;
			}
            if(width<=0){
                Toast.makeText(ma,"宽度必须>0",0).show();
                return;
            }else if(width>=sizeLim){
                Toast.makeText(ma,"宽度过大",0).show();
                return;
            }
            if(height<=0){
                Toast.makeText(ma,"高度必须>0",0).show();
                return;
            }else if(height>=sizeLim){
                Toast.makeText(ma,"高度过大",0).show();
                return;
            }
            String filename=editor3.getEditableText().toString();
            if(filename.contains("/")){
                Toast.makeText(ma,"文件名不能包含/",0).show();
                return;
            }
            
            
            if(filename.isEmpty()){
                Bitmap image=Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
                ImageManipulationTab imt=new ImageManipulationTab(ma);
                ImageManipulationPage imp=new ImageManipulationPage(ma,image);
                imt.imp=imp;
                imt.setText("新图像");
                imt.setOnClickListener(ma.gui.TabOnClick);
                ma.gui.tabs.addView(imt,TabBar.tablp);
                ma.gui.TabOnClick.onClick(imt);
                ma.wm.removeView(this);
            }else{
                File f;
                if(destination==null){
                    f=new File(ma.workDir,filename);
                }else{
                    f=new File(destination,filename);
                }
                if(f.exists()){
                    Toast.makeText(ma,"文件已存在",0).show();
                    return;
                }
                try{
                    if(f.createNewFile()){
                        Bitmap image=Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
                        FileOutputStream fos=new FileOutputStream(f,false);
                        image.compress(Bitmap.CompressFormat.PNG,100,fos);
                        ImageManipulationTab imt=new ImageManipulationTab(ma);
                        ImageManipulationPage imp=new ImageManipulationPage(ma,image);
                        imt.imp=imp;
                        imt.im=new TabData.ImageManipulation();
                        imt.im.src=f;
                        imt.setText(f.getName());
                        imt.setOnClickListener(ma.gui.TabOnClick);
                        ma.gui.tabs.addView(imt,TabBar.tablp);
                        ma.gui.TabOnClick.onClick(imt);
                        ma.wm.removeView(this);
                    }else{
                        Toast.makeText(ma,"创建文件失败",0).show();
                    }
                }catch(Exception e){
                    log.alert(e.toString());
                }
            }
		}
	}
	@Override
	protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
		return true;
	}
}
